<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

/**
 * Service class for website management
 */

class m_website_services_WebsiteService
{
	/**
	 * @var m_website_services_WebsiteService
	 */
	private static $instance;
	
	/**
	 * Constructor
	 */
	private function __construct(){}
	
	/**
	 * @return m_website_services_WebsiteService
	 */
	public static function getInstance()
	{
		if (self::$instance === null)
		{
			self::$instance = new self();
		}
		return self::$instance;
	}
	
	/**
	 * Retrieve all websites
	 *
	 * @return array
	 */
	public function getAllWebsites()
	{
		$documentProvider = f_document_Provider::getInstance();
		
		return $documentProvider->loadDocuments("modules_website/website")->retrieveDocuments();
	}

    /**
     * Get website document from host
     *
     * @return m_website_documents_Website
     */
	public function getWebsiteFromHost()
	{	
		$documentProvider = f_document_Provider::getInstance();
		return $documentProvider->loadDocuments("modules_website/website")->whereIsEqual("url", URL_ABSOLUTE_WO_HTTP)->retrieveFirstDocument();
	}

    /**
     * Get all frontend themes names
     *
     * @return array
     */
    public function getFrontThemes()
    {
        $xml = f_file_Resolver::getInstance()->fromDirectory(HTTP_DIR.DS.'themes')->setRecursive(true)->setMask('frontend'.DS.'layouts'.DS.'*.xml')->find();

        $themes = array();

        if(count($xml) > 0)
        {
            foreach($xml as $file)
            {
                $path = explode(DS, $file);
                $name = $path[count($path) - 4];
                $themes[] = $name;
            }
            $themes = array_unique($themes);
        }

        return $themes;
    }

    /**
     * Get complete html site map from the current website (getted from host)
     *
     * @param boolean $showAliases
     * @return html
     */
    public function getSiteMap($showAliases = false)
    {
        $relManager = f_relation_Manager::getInstance();
        
        $website = $this->getWebsiteFromHost();
        $lang = f_core_Context::getInstance()->getRequestLang();

        $siteMap = '<div class="pz-block pz-block-siteMap">'."\r\n";
        $siteMap .= '<ul>'."\r\n";
        $siteMap .= $this->getSiteMapItems($website, 1, $showAliases);
        $siteMap .= '</ul>';
        $siteMap .= '</div>';

        return $siteMap;
    }

    /**
     * Get a sitemap node
     *
     * @param f_document_BaseDocument $node
     * @param integer $level
     * @param boolean $showAliases
     * @return html
     */
    private function getSiteMapItems($node, $level = 1, $showAliases)
    {
        $result = "";
        
        $relManager = f_relation_Manager::getInstance();

        $childs = $relManager->getChildsByDocType($node->getUniqueId(), array("modules_website/menu", "modules_website/page", "modules_backoffice/alias"), $node->getLang(), null, "PUBLISHED");

        $spaces = "\t";
        for ($i = 1 ; $i < $level ; $i++)
        {
            $spaces .= "\t";
        }

        foreach($childs as $child)
        {
            if($child->getModel() == 'modules_backoffice/alias' && $showAliases == true)
            {
                $child = $child->getOriginalDocument();
                $child->setInSiteMap(true);
            }
            
            if($child->getModel() == "modules_website/page" && $child->isInSiteMap())
            {
                $result .= $spaces.'<li class="siteMapPage"><a href="'.$child->getUrl().'">'.ucfirst($child->getMenuLabel())."</a></li>"."\r\n";
            }

            if($child->getModel() == "modules_website/menu" && $child->isVisibleInSiteMap())
            {
                $targets = $child->getDocumentTargets();

                $className = '';

                if(count($targets) > 0)
                {
                    $className = "";
                    foreach($targets as $target)
                    {
                        $className .= "siteMap_".$target["label"]." ";
                    }
                    $className = trim($className);
                }

                $child->getUrl() != "" ? $link = '<a href="'.$child->getUrl().'">'.ucfirst($child->getMenuLabel())."</a>" : $link = $child->getMenuLabel();

                $class = ' class="siteMapSubMenuTitle '.$className.'"';

                $result .= $spaces."<li".$class."><span".$class.">".$link."</span>\r\n";

                $res = $this->getSiteMapItems($child, $level++, $showAliases);

                if($res != "")
                {
                    $class = ' class="siteMapSubMenu '.$className.'"';
                    $result .= $spaces."\r\n$spaces<ul".$class.">\r\n".$res."$spaces</ul>"."\r\n";
                }

                $result .= "</li>";
            }
        }

        return $result;
    }
}